package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.service.ResourceService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.MenuVo;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resource")
@Api(tags = "资源管理")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private UserThreadLocal userThreadLocal;

    @PostMapping("/list")
    @ApiOperation("查询资源列表")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.parentResourceNo", "resourceDto.resourceType"})
    @Cacheable(value = "Resource",key = "#resourceDto.hashCode()")
    public ResponseResult list(@RequestBody ResourceDto resourceDto){
        List<ResourceVo> list = resourceService.list(resourceDto);
        return ResponseResult.success(list);
    }

    @PostMapping("/tree")
    @ApiOperation("查询资源层级关系")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = false, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.label"})
    @Cacheable(value = "Tree",key = "3")
    public ResponseResult tree(@RequestBody ResourceDto resourceDto){
        TreeVo vo = resourceService.tree(resourceDto);
        return ResponseResult.success(vo);
    }

    @PutMapping
    @ApiOperation("资源添加")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.icon"
            , "resourceDto.parentResourceNo"
            , "resourceDto.requestPath"
            , "resourceDto.resourceName"
            , "resourceDto.resourceType"
            , "resourceDto.sortNo"})
    @Caching(evict = {
            @CacheEvict(value = "Resource",allEntries = true),
            @CacheEvict(value = "Tree",key = "3")
    })
    public ResponseResult createResource(@RequestBody ResourceDto resourceDto) {
        resourceService.createResource(resourceDto);
        return ResponseResult.success();
    }

    @PatchMapping
    @ApiOperation("资源修改")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.icon"
            , "resourceDto.parentResourceNo"
            , "resourceDto.requestPath"
            , "resourceDto.resourceName"
            , "resourceDto.resourceType"
            , "resourceDto.sortNo"
            , "resourceDto.id"})
    @Caching(evict = {
            @CacheEvict(value = "Resource",allEntries = true),
            @CacheEvict(value = "Resource",key = "3"),
            @CacheEvict(value = "Resource::menus",allEntries = true)
    })
    public ResponseResult updateResource(@RequestBody ResourceDto resourceDto){
        resourceService.updateResource(resourceDto);
        return ResponseResult.success();
    }

    @GetMapping("/menus")
    @ApiOperation("获取动态菜单资源")
    @CachePut(value = "Resource::menus",key = "#result.getData().get(0).getId()")
    public ResponseResult getMenusById(){
        List<MenuVo> list = resourceService.getMenusById();
        return ResponseResult.success(list);
    }
}
